package com.alibaba.citrus.ots.b2b.trade.order.repository;

import com.alibaba.citrus.ots.common.annotation.EpochTransactional;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.bcots.platform_enhance.SaleOrderTag;
import com.epoch.app.otsb2btradecenter.domain.order.model.Order;
import com.epoch.app.otsb2btradecenter.domain.orderline.model.OrderLine;
import com.epoch.app.otsb2btradecenter.enums.OrderStatus;
import com.epoch.app.otsb2btradecenter.orderbackup.model.OrderBackUp;
import com.epoch.app.otsb2btradecenter.orderbackup.service.OrderBackUpService;
import com.epoch.app.otsb2btradecenter.orderlinebackup.model.OrderLineBackUp;
import com.epoch.app.otsb2btradecenter.orderlinebackup.service.OrderLineBackUpService;
import com.epoch.app.otsb2btradecenter.sdo.OrderLineSDO;
import com.epoch.app.otsb2btradecenter.sdo.OrderSDO;
import com.epoch.app.otsb2btradecenter.sdo.PaymentInformationSDO;
import com.epoch.app.otsb2btradecenter.sdo.ScItemSDO;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Objects;

@Component
public class OrderBackUpRepositoryImpl implements OrderBackUpRepository{

    @Resource
    private OrderBackUpService orderBackUpService;
    @Resource
    private OrderLineBackUpService orderLineBackUpService;
    @Resource
    private OrderWriteRepository orderWriteRepository;
    @Resource
    private OrderLineWriteRepository orderLineWriteRepository;

    @EpochTransactional(appCode="ots_b2b_trade_center", dataSourceCode = "cr_ots")
    @Override
    public void deleteOrder(OrderSDO orderSDO) {
        OrderBackUp orderBackUp = createOrderConverter(orderSDO);
        orderBackUpService.create(orderBackUp);
        for (OrderLineSDO orderLineSDO : orderSDO.getOrderLineSDOS()) {
            OrderLineBackUp orderLineBackUp = createOrderLineConverter(orderLineSDO);
            orderLineBackUpService.create(orderLineBackUp);
        }
        orderWriteRepository.deleteOrder(orderSDO.getId());
        for (OrderLineSDO orderLineSDO : orderSDO.getOrderLineSDOS()) {
            orderLineWriteRepository.remove(orderLineSDO);
        }
    }

    private OrderBackUp createOrderConverter(OrderSDO orderSDO) {
        OrderBackUp createRequest = new OrderBackUp();
        createRequest.setId(Long.parseLong(orderSDO.getId()));
        createRequest.setFrontOrderId(orderSDO.getFrontOrderId());
        createRequest.setAreaCode(orderSDO.getAreaCode());
        createRequest.setBisUnitCode(orderSDO.getBisUnitCode());
        createRequest.setSalesmanId(orderSDO.getSalesmanId());
        createRequest.setSalesmanName(orderSDO.getSalesmanName());
        createRequest.setOrderBizId(orderSDO.getOrderBizId());
        createRequest.setOrderBizId(orderSDO.getOrderBizId());
        createRequest.setOuterOrderId(orderSDO.getOuterOrderId());
        createRequest.setBizCode(orderSDO.getBizCode());
        createRequest.setBusinessCode(orderSDO.getBusinessCode());
        createRequest.setOrderType(orderSDO.getOrderType());
        //TODO 销售订单名称
        createRequest.setOrderTag(orderSDO.getOrderTag());
        createRequest.setCustomerCode(orderSDO.getCustomerCode());
        createRequest.setCustomerName(orderSDO.getCustomerName());
        createRequest.setOrganizationCode(orderSDO.getOrganizationCode());
        createRequest.setOrganizationName(orderSDO.getOrganizationName());
        createRequest.setDepartmentCode(orderSDO.getDepartmentCode());
        createRequest.setDepartmentName(orderSDO.getDepartmentName());
        createRequest.setProductGroupCode(orderSDO.getProductGroupCode());
        createRequest.setProductGroupName(orderSDO.getProductGroupName());
        createRequest.setSaleChannelCode(orderSDO.getSaleChannelCode());
        createRequest.setSaleChannelName(orderSDO.getSaleChannelName());
        createRequest.setChannelCode(orderSDO.getChannelCode());
        createRequest.setChannelName(orderSDO.getChannelName());
        createRequest.setReceiveAddressCode(orderSDO.getReceiveAddressCode());
        createRequest.setReceiveDetailAddress(orderSDO.getReceiveDetailAddress());
        createRequest.setStatus(orderSDO.getStatus());
        createRequest.setOrderTag(orderSDO.getOrderTag());
        createRequest.setCustomerAccounts(orderSDO.getCustomerAccounts());

        PaymentInformationSDO paymentInformationSDO = orderSDO.getPaymentInformationSDO();
        if (paymentInformationSDO != null) {
            createRequest.setOrderTotalFee(paymentInformationSDO.getOrderTotalFee());
            createRequest.setActualPaidFee(paymentInformationSDO.getActualPaidFee());
            createRequest.setShouldPayFee(paymentInformationSDO.getShouldPayFee());
            createRequest.setPostFee(paymentInformationSDO.getPostFee());
            createRequest.setPayStatus(paymentInformationSDO.getPayStatus());
            createRequest.setPayTime(paymentInformationSDO.getPayTime());
        }

        createRequest.setDeliveryStatus(OrderStatus.BEFORE_DELIVERY.getCode());
        createRequest.setEndTime(orderSDO.getEndTime());
        createRequest.setTransportModeCode(orderSDO.getTransportModeCode());
        createRequest.setTransportModeName(orderSDO.getTransportModeName());
        createRequest.setWareHouseCode(orderSDO.getWareHouseCode());
        createRequest.setWareHouseName(orderSDO.getWareHouseName());
        createRequest.setCustomerReferenceNo(orderSDO.getCustomerReferenceNumber());
        createRequest.setCustomerExpectDate(orderSDO.getCustomerExpectDate());
        createRequest.setCostCenter(orderSDO.getCostCenter());
        createRequest.setCashCustomerName(orderSDO.getCashCustomerName());
        createRequest.setSyncVersion(orderSDO.getSyncVersion());
        createRequest.setReverseReason(orderSDO.getReverseReason());
        createRequest.setCreateReason(orderSDO.getCreateReason());
        createRequest.setRemark(orderSDO.getRemark());
        createRequest.setGmtCreate(orderSDO.getGmtCreate());
        createRequest.setGmtModified(orderSDO.getGmtModified() == null ? new Date() : orderSDO.getGmtModified());
        createRequest.setCreatorName(orderSDO.getCreatorName());
        createRequest.setModifier(orderSDO.getModifierId());
        createRequest.setModifierName(orderSDO.getModifierName());
        createRequest.setContact(orderSDO.getContactor());
        createRequest.setContactPhone(orderSDO.getContactorPhone());
        createRequest.setSyncVersion(orderSDO.getSyncVersion());

        String featuresStr = JSONObject.toJSONString(MapUtils.isEmpty(orderSDO.getFeatures()) ? new HashMap<>() : orderSDO.getFeatures());
        createRequest.setFeatures(featuresStr);
        return createRequest;
    }

    private OrderLineBackUp createOrderLineConverter(OrderLineSDO orderLineSDO) {
        OrderLineBackUp orderLine = new OrderLineBackUp();
        if (Objects.nonNull(orderLineSDO.getScItemSDO())) {
            orderLine.setScItemOutCode(orderLineSDO.getScItemSDO().getScItemOutCode());
        }
        orderLine.setId(Long.parseLong(orderLineSDO.getId()));
        orderLine.setMainOrderBizId(orderLineSDO.getMainOrderBizId());
        orderLine.setSerialNumber(orderLineSDO.getSerialNumber());
        orderLine.setMainOrderId(orderLineSDO.getMainOrderId());
        orderLine.setOrderLineTag(orderLineSDO.getOrderLineTag());
        orderLine.setBizCode(orderLineSDO.getBizCode());
        orderLine.setOuterOrderLineId(orderLineSDO.getOuterOrderLineId());
        orderLine.setCustomerCode(orderLineSDO.getCustomerCode());
        orderLine.setStatus(orderLineSDO.getStatus());
        orderLine.setPayStatus(orderLineSDO.getPayStatus());
        orderLine.setPayTime(orderLineSDO.getPayTime());

        ScItemSDO scItemSDO = orderLineSDO.getScItemSDO();

        if (scItemSDO != null) {
            orderLine.setCategoryCode(scItemSDO.getCategoryCode());
            orderLine.setCategoryDesc(scItemSDO.getCategoryDesc());
            orderLine.setCategoryChildCode(scItemSDO.getCategoryChildCode());
            orderLine.setCategoryChildDesc(scItemSDO.getCategoryChildDesc());

            orderLine.setFirstCategoryCode(scItemSDO.getFirstCategoryCode());
            orderLine.setFirstCategoryDesc(scItemSDO.getFirstCategoryDesc());
            orderLine.setSecondCategoryCode(scItemSDO.getSecondCategoryCode());
            orderLine.setSecondCategoryDesc(scItemSDO.getSecondCategoryDesc());
            orderLine.setSaleUnit(scItemSDO.getSaleUnit());
            orderLine.setSaleUnitName(scItemSDO.getSaleUnitName());
            orderLine.setInventoryUnitCode(scItemSDO.getInventoryUnitCode());
            orderLine.setInventoryUnit(scItemSDO.getInventoryUnitName());
            orderLine.setUnitConvert(scItemSDO.getUnitConvert());
            orderLine.setUnitPrice(scItemSDO.getUnitPrice() == null ? null :scItemSDO.getUnitPrice().toString());
            orderLine.setQuantity(scItemSDO.getQuantity());
            orderLine.setActivityPrice(scItemSDO.getActivityPrice() == null ? null :scItemSDO.getActivityPrice().toString());
            orderLine.setScItemId(scItemSDO.getScItemId());
            orderLine.setScItemTitle(scItemSDO.getScItemTitle());
            orderLine.setProductGroupCode(scItemSDO.getProductGroupCode());
            orderLine.setProductGroupName(scItemSDO.getProductGroupName());
            orderLine.setProductationTime(scItemSDO.getProductationTime());
        }
        orderLine.setShouldPayFee(null == orderLineSDO.getShouldPayFee() ? null : orderLineSDO.getShouldPayFee().toString());
        orderLine.setActualPaidFee(null == orderLineSDO.getActualPaidFee() ? null : orderLineSDO.getActualPaidFee().toString());
        orderLine.setDeliveryStatus(orderLineSDO.getDeliveryStatus());
        orderLine.setDeliveryTime(orderLineSDO.getDeliveryTime());
        orderLine.setEndTime(orderLineSDO.getEndTime());
        orderLine.setActivityCode(orderLineSDO.getActivityCode());
        orderLine.setActivityName(orderLineSDO.getActivityName());
        orderLine.setFrontOrderLineId(orderLineSDO.getFrontOrderLineId());
        orderLine.setRemark(orderLineSDO.getRemark());
        orderLine.setSyncVersion(orderLineSDO.getSyncVersion());

        orderLine.setRefuseReasonName(orderLineSDO.getRefuseReasonName());
        orderLine.setRefuseReasonCode(orderLineSDO.getRefuseReasonCode());
        orderLine.setDeliveryAmount(orderLineSDO.getDeliveryAmount());
        orderLine.setGmtCreate(orderLineSDO.getGmtCreate());
        orderLine.setGmtModified(orderLineSDO.getGmtModified() == null? new Date() : orderLineSDO.getGmtModified());
        orderLine.setCreatorId(orderLineSDO.getCreatorId());
        orderLine.setCreatorName(orderLineSDO.getCreatorName());
        orderLine.setModifierId(orderLineSDO.getModifierId());
        orderLine.setModifierName(orderLineSDO.getModifierName());
        orderLine.setReturnCharacter(orderLineSDO.getReturnCharacter());
        orderLine.setOrderLineTag(orderLineSDO.getOrderLineTag());
        orderLine.setCumulativeReceiptQuantity(orderLine.getCumulativeReceiptQuantity());
        orderLine.setCumulativeProcessingQuantity(orderLine.getCumulativeProcessingQuantity());

        if (MapUtils.isNotEmpty(orderLineSDO.getFeatures())) {
            String featureStr = JSONObject.toJSONString(orderLineSDO.getFeatures());
            orderLine.setFeatures(featureStr);
        }
        return orderLine;
    }
}
